home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 201-225 / disk_203 / gurusguide / intrsup.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  4KB  |  150 lines

  1. /************************************************************************
  2. **********                                                     **********
  3. **********          I N T E R R U P T   S U P P O R T          **********
  4. **********          ---------------------------------          **********
  5. **********                                                     **********
  6. **********        Copyright (C) 1988 Sassenrath Research       **********
  7. **********                All Rights Reserved.                 **********
  8. **********                                                     **********
  9. **********    Example from the "Guru's Guide, Meditation #1"   **********
  10. **********                                                     **********
  11. /************************************************************************
  12. **                                                                     **
  13. **                            - NOTICE -                               **
  14. **                                                                     **
  15. **  The "Guru's Guide, Meditation #1" contains detailed information    **
  16. **  about Amiga interrupts as well as a complete discussion of this    **
  17. **  and other examples.  Meditation #1 and all of its examples were    **
  18. **  written by Carl Sassenrath, the architect of Amiga's multitasking  **
  19. **  operating system.  Copies of the "Guru's Guide" may be obtained    **
  20. **  from:                                                              **
  21. **           GURU'S GUIDE, P.O. BOX 1510, UKIAH, CA 95482              **
  22. **                                                                     **
  23. **  Please include a check for $14.95, plus $1.50 shipping ($4.00 if   **
  24. **  outside North America).  CA residents add 6% sales tax.            **
  25. **                                                                     **
  26. **  This example may be used for any purposes, commercial, personal,   **
  27. **  public, and private, so long as ALL of the above text, copyright,  **
  28. **  mailing address, and this notice are retained in their entirety.   **
  29. **                                                                     **
  30. **  THIS EXAMPLE IS PROVIDED WITHOUT WARRANTY OF ANY KIND.             **
  31. **                                                                     **
  32. ************************************************************************/
  33.  
  34. /*
  35. **  COMPILATION NOTE:
  36. **
  37. **  Compiled under MANX AZTEC C 3.6A.  Use the +L compiler option.
  38. **  This file is linked with most of the other interrupt examples.
  39. */
  40.  
  41. #include <exec/interrupts.h>
  42. #include <exec/memory.h>
  43. #include <hardware/custom.h>
  44. #include <hardware/intbits.h>
  45.  
  46. /*
  47. **    Interrupt Controller Functions
  48. */
  49. EnableIntr(intrNum)
  50.     int intrNum;
  51. {
  52.     custom.intena = INTF_SETCLR | (1 << intrNum);
  53. }
  54.  
  55.  
  56. DisableIntr(intrNum)
  57.     int intrNum;
  58. {
  59.     custom.intena = (1 << intrNum);
  60. }
  61.  
  62.  
  63. RequestIntr(intrNum)
  64.     int intrNum;
  65. {
  66.     custom.intreq = INTF_SETCLR | (1 << intrNum);
  67. }
  68.  
  69.  
  70. ClearIntr(intrNum)
  71.     int intrNum;
  72. {
  73.     custom.intreq = (1 << intrNum);
  74. }
  75.  
  76.  
  77. /*
  78. **    Structure Functions
  79. */
  80. struct Interrupt *MakeIntr(name,pri,code,data)
  81.     char *name;
  82.     int  pri;
  83.     VOID (*code)();
  84.     APTR data;
  85. {
  86.     extern struct Interrupt *AllocMem();
  87.     register struct Interrupt *intr;
  88.  
  89.     intr = AllocMem(sizeof(struct Interrupt), MEMF_PUBLIC);
  90.     if (intr == NULL) return NULL;
  91.  
  92.     intr->is_Node.ln_Pri = pri;
  93.     intr->is_Node.ln_Type = NT_INTERRUPT;
  94.     intr->is_Node.ln_Name = name;
  95.     intr->is_Data = data;
  96.     intr->is_Code = code;
  97.  
  98.     return intr;
  99. }
  100.  
  101.  
  102. FreeIntr(intr)
  103.     register struct Interrupt *intr;
  104. {
  105.     if (intr == NULL) return;
  106.     if (intr->is_Node.ln_Type != NT_INTERRUPT) return;
  107.  
  108.     intr->is_Node.ln_Type = 0;
  109.  
  110.     FreeMem(intr, sizeof(struct Interrupt));
  111. }
  112.  
  113.  
  114. /*
  115. **    Handler Functions
  116. */
  117.  
  118. extern struct Interrupt *SetIntVector();
  119.  
  120. struct Interrupt *AddHandler(intrNum,intr)
  121.     int intrNum;
  122.     struct Interrupt *intr;
  123. {
  124.     DisableIntr(intrNum);
  125.     ClearIntr(intrNum);
  126.     SetIntVector(intrNum,intr);    /* ignore result */
  127. }
  128.  
  129.  
  130. RemHandler(intrNum,intr)
  131.     int intrNum;
  132.     register struct Interrupt *intr;
  133. {
  134.     register struct Interrupt *retIntr;
  135.  
  136.     if (intr == NULL) return NULL;
  137.  
  138.     /*    CRITICAL SECTION: (See text)
  139.     **    Mutually exclude all tasks and interrupts.
  140.     **    Remove the handler if it is really ours
  141.     **    (it could have been replaced already!)
  142.     **    Disable the interrupt if it is ours.
  143.     */
  144.     Disable();    
  145.     retIntr = SetIntVector(intrNum,NULL);
  146.     if (retIntr != intr) SetIntVector(intrNum,retIntr);
  147.     else DisableIntr(intrNum);
  148.     Enable();
  149. }
  150.